package com.xjkj.workflow.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjkj.workflow.entity.Result;
import com.xjkj.workflow.entity.User;
import com.xjkj.workflow.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "工作流用户接口")
@RestController
@RequestMapping("/v1")
public class UserController {

    @Autowired
    UserService userService;


    @ApiOperation("开始工作流")
    @GetMapping("/startProcess")
    @ResponseBody
    public void startProcess(String username, String processName) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //不同接口加载不同bpmn文件执行不同流程
        //不执行部署，也可以直接开始流程，但是PRODEF和DEPLOYMENT表内会没数据，只有EXECUTION有，历史表里也没有
        Deployment deployment = repositoryService.createDeployment().
                addClasspathResource("processes/MyProcess.bpmn")
                .addClasspathResource("processes/MyProcess.png")
                .deploy();
        Deployment deploymentB = repositoryService.createDeployment().
                addClasspathResource("processes/MyProcess.bpmn")
                .addClasspathResource("processes/MyProcess.png")
                .deploy();
//        securityUtil.logInAs(username);
        //startProcessInstanceById()是就方法，不用
        System.out.printf("111");
        //ProcessInstance对用RU——Execution表
        ProcessInstance p = processEngine.getRuntimeService().startProcessInstanceByKey("flowA");
        ProcessInstance pB = processEngine.getRuntimeService().startProcessInstanceByKey("flowB");
        if(pB == null){
            System.out.printf("222");
        }
        System.out.println("流程实例ID：" + p.getId());
        System.out.println("流程实例名字：" + p.getName());
        System.out.println("流程实例用户：" + p.getStartUserId());
        System.out.printf("===========================");
        System.out.println("流程实例ID：" + pB.getId());
        System.out.println("流程实例名字：" + pB.getName());
        System.out.println("流程实例用户：" + pB.getStartUserId());
    }


    @ApiOperation("查询用户基本数据")
    @GetMapping("/getUser")
    public Result<User> getBasicData() {
        Result result = new Result();
        User user = userService.getOne(new QueryWrapper<>());
        result.setResult(user);
        return result;
    }

}
